package com.nine.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nine.shop.common.ServerResponse;
import com.nine.shop.mapper.CartMapper;
import com.nine.shop.model.Cart;
import com.nine.shop.model.Goods;
import com.nine.shop.model.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wangcong
 * @Date: 2019/8/25 10:28
 * @Description:
 */
@Service
public class CartService extends ServiceImpl<CartMapper, Cart> {

    @Resource
    private GoodsService goodsService;

    /**
     * Describe: 查询用户的购物车信息
     *
     * @return
     * @author 张新飞
     * @date 10:59 2019/8/29
     * @parms * @param null
     */
    public ServerResponse searchShopcart(User user) {
        //根据用户id，查询用户的购物车信息
        List<Map<String, Object>> list = listMaps(new QueryWrapper<Cart>().eq("userId", user.getId()));
        System.out.println("查看购物车信息：" + list);
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            //查询根据商品id查询商品信息
            Goods goods = goodsService.getById((int) map.get("goodsId"));
            System.out.println("查看商品信息：" + goods);
            //把商品信息封装到map里面
            map.put("goods", goods);
        }
        if (list.size() > 0) {
            return ServerResponse.createBySuccess(list);
        }
        return ServerResponse.createByErrorMsg("查询错误！");
    }

    /**
     * @Description 分页查询购物车信息
     * @Param [current, size, userId]
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.nine.shop.model.Cart>
     * @Author wangcong
     * @Date 2019/8/25
     * @Time 10:48
     */
    public IPage<Cart> searchByIdPage(Integer current, Integer size, Integer userId) {
        return baseMapper.selectPage(new Page<>(current, size), new QueryWrapper<Cart>().eq("userId", userId));
    }

    /**
     * @Description 查询购物车信息
     * @Param [userId]
     * @Return java.util.List<com.nine.shop.model.Cart>
     * @Author wangcong
     * @Date 2019/8/25
     * @Time 11:19
     */
    public List<Cart> searchById(int userId) {
        return baseMapper.serrchAllCart(userId);//selectList(new QueryWrapper<Cart>().eq("userId",userId));
    }
    public int deletegoodsbyid(int cartid){
        return baseMapper.deleteById(cartid);
    }
    /**
     * @Description 更新购物车信息
     * @Param [userId, cart]
     * @Return java.lang.Boolean
     * @Author wangcong
     * @Date 2019/8/25
     * @Time 13:57
     */
    public Boolean updategoodsinfo(Cart cart) {
        return baseMapper.update(cart, new QueryWrapper<Cart>().
                and(i -> i.eq("userId", cart.getUserId()).eq("goodsId", cart.getGoodsId()))) > 0;
    }

    /**
     * @Description 清空购物车
     * @Param [userId, goodsId]
     * @Return int
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 11:05
     */
    public int clearnCart(int userId, int goodsId) {
        Cart cart = new Cart();
        cart = baseMapper.selectOne(new QueryWrapper<Cart>().and(i -> i.eq("userId", userId).eq("goodsId", goodsId)));
        return baseMapper.deleteById(cart.getId());

    }


    /**
     * 根据用户ID和商品ID查询购物车是否存在
     */
    public ServerResponse same(Integer userid, Integer goodsid, Integer amount) {
        Cart cart = new Cart();
        List<Cart> list = list(new QueryWrapper<Cart>().and(i -> i.eq("userid", userid).eq("goodsid", goodsid)));
        if (list.size() >= 1) {
            System.out.println("数据库中已经存在本商品,进行数量添加");
            //购物车中已经存在了本商品,进行商品数量添加
            cart = list.get(0);
            cart.setAmount(cart.getAmount() + amount);
            if (baseMapper.updateById(cart) == 0) {
                return ServerResponse.createBySuccess(cart);
            }
        } else {
            cart.setUserId(userid);
            cart.setGoodsId(goodsid);
            cart.setAmount(amount);
            cart.setCreateDate(new Date());
            if (baseMapper.insert(cart) == 0) {
                return ServerResponse.createBySuccess(cart);
            }
        }
        return ServerResponse.createByErrorMsg("添加失败");

    }


}
